//
//  ForgetPasswordViewController.m
//  InviteJob
//
//  Created by LangLang on 17/10/31.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "RegistViewController.h"
#import "UIButton+Timer.h"
#import "WebViewViewController.h"
@interface RegistViewController ()


@property (weak, nonatomic) IBOutlet UIView *agreementView;
@property (weak, nonatomic) IBOutlet UIButton *btnCode;

@property (weak, nonatomic) IBOutlet UITextField *txtMoblie;
@property (weak, nonatomic) IBOutlet UITextField *txtCode;
@property (weak, nonatomic) IBOutlet UITextField *txtPasswd;
@property (weak, nonatomic) IBOutlet UITextField *txtSurePasswd;
@property (weak, nonatomic) IBOutlet UITextField *txtRecommendMoblie;


@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    TitleView(@"注册")
    
    [self codeUI];
    [self userAgreementText];
}

-(void)userAgreementText{
    UIButton *buttonAress = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonAress setTitle:@"我已阅读并同意《用户协议》" forState:UIControlStateNormal];
    [buttonAress setTitleColor:NavBackColor forState:UIControlStateNormal];
    [buttonAress addTarget:self action:@selector(OnClickAress) forControlEvents:UIControlEventTouchUpInside];
    [self setLabelColor:buttonAress andStr1:buttonAress.titleLabel.text with:strColor(@"333")];
    buttonAress.titleLabel.font = [UIFont systemFontOfSize:13];
    [_agreementView addSubview:buttonAress];
    NSString *content = buttonAress.titleLabel.text;
    UIFont *font = buttonAress.titleLabel.font;
    CGSize size = CGSizeMake(MAXFLOAT, 30.0f);
    CGSize buttonSize = [content boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine  | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                           attributes:@{ NSFontAttributeName:font}
                                              context:nil].size;
    buttonAress.frame = CGRectMake(self.view.frame.size.width/2-buttonSize.width/2,10, buttonSize.width, 20);
    UIButton *buttonIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonIcon setImage:[UIImage imageNamed:@"icon_login_check"] forState:UIControlStateNormal];
    buttonIcon.imageView.contentMode = UIViewContentModeScaleAspectFill;
    buttonIcon.layer.masksToBounds = YES;
    buttonIcon.imageView.clipsToBounds = YES;
    [_agreementView addSubview:buttonIcon];
    [buttonIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(buttonAress.mas_left).offset(-18);
        make.top.equalTo(buttonAress.mas_top);
        make.height.width.equalTo(@20);
    }];
}
// 协议
-(void)OnClickAress{
    NSString *strUrl = [NSString stringWithFormat:user_registerDoc_code_api];

    [PostNetWorking postNetWorkingStr:strUrl andparameters:nil Success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            WebViewViewController *webView = [WebViewViewController new];
            webView.strUrl = responseObject[@"datas"][@"url"];
            [self.navigationController pushViewController:webView animated:YES];
            
        }else{
            NSString *mark = [NSString stringWithFormat:@"%@",responseObject[@"mark"]];
            SHuiMBhudTextTimeStr(mark);
        }
        
    } failed:^(NSError *error) {
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
}


-(void)setLabelColor:(UIButton *)strLabel andStr1:(NSString *)str1 with:(UIColor *)color{
    if (str1.length!=0) {
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:str1];
        [str addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0,7)];
        [strLabel setAttributedTitle:str forState:(UIControlStateNormal)];
    }
    
}

-(void)codeUI{
    
    _btnCode.layer.cornerRadius = 3;
    _btnCode.layer.borderWidth = 0.5;
    _btnCode.layer.borderColor = NavBackColor.CGColor;
    _btnCode.layer.masksToBounds = YES;
    
     _txtPasswd.secureTextEntry = YES;
     _txtSurePasswd.secureTextEntry = YES;
    
   }
- (IBAction)btnCodeOnClick:(id)sender {
    if ([SHJudgeTools valiMobile:_txtMoblie.text]) {
        SHuiMBhudTextTimeStr(@"请输入正确的手机号");
        return;
    }
    [self loadRegist];
    
}

// 获取验证码
-(void)loadRegist{
    
    NSString *strUrl = [NSString stringWithFormat:user_signup_code_api];
    NSDictionary *parameter = @{@"phone":_txtMoblie.text};
    [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            _btnCode.time = 60;
            _btnCode.format = @"%lds";
            [_btnCode startTimer];
            SHuiMBhudTextTimeStr(@"发送成功");
        }else{
            NSString *mark = [NSString stringWithFormat:@"%@",responseObject[@"mark"]];
            SHuiMBhudTextTimeStr(mark);
        }
        
    } failed:^(NSError *error) {
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
    
}
- (IBAction)btnRegist:(id)sender {
    
    if ([SHJudgeTools valiMobile:_txtMoblie.text]) {
        SHuiMBhudTextTimeStr(@"请输入手机号");
        return;
    }
    if (_txtCode.text.length == 0) {
        SHuiMBhudTextTimeStr(@"请输入验证码");
        return;
    }
    if (![SHJudgeTools validatePassword:_txtPasswd.text]) {
        SHuiMBhudTextTimeStr(@"请输入6-20位密码");
        return;
    }
    if (_txtPasswd.text != _txtSurePasswd.text) {
        SHuiMBhudTextTimeStr(@"两次输入密码不一致");
        return;
    }
    NSString *strUrl = [NSString stringWithFormat:user_signup_api];
    NSDictionary *parameter;
    if (_txtRecommendMoblie.text.length==0) {
        parameter = @{@"phone":_txtMoblie.text,
                      @"password":_txtPasswd.text,
                      @"code":_txtCode.text};
    }else{
        parameter = @{@"phone":_txtMoblie.text,
                      @"password":_txtPasswd.text,
                      @"code":_txtCode.text,
                      @"p_phone":_txtRecommendMoblie.text};
    }
    [MBProgressHUD showMessag:@"注册中..." withDelay:0 toView:self. view withMode:MBProgressHUDModeIndeterminate hideWithDelay:0];
    [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
        SHuiMBHidleYes
        if ([responseObject[@"code"] intValue] == 200) {
            SHuiMBhudTextTimeStr(@"注册成功");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
                
            });
        }else{
          
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
    
}





@end
